﻿/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shuriken.Core
{
    /// <summary>
    /// Class for storing command line parameters 
    /// </summary>
    /// <remarks>
    /// The common form of this params looks the following /key:value
    /// key is the key for parameter 
    /// value is the value for the parameter 
    /// </remarks>
    public class CommandLineParameters
    {
        string[] m_CommandLineParameters;
        Dictionary<string, string> m_ParameterDictionary; 

        public CommandLineParameters(string[] args)
        {
            m_ParameterDictionary = new Dictionary<string, string>();

            foreach(string arg in args)
            {
                string key = GetKeyFromParam(arg);
                string val = GetValueFromParam(arg);

                switch (key)
                {
                    case "/delay":

                        int iDelay = 0; 

                        if (String.IsNullOrEmpty(val) && int.TryParse(val,out iDelay))
                        {
                            if (!m_ParameterDictionary.ContainsKey("DelayedStart"))
                            {
                                m_ParameterDictionary.Add("DelayedStart", val);
                            }
                        }
                        break;

                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// Provides the delay time in miliseconds at startup 
        /// </summary>
        public int DelayedStart
        {
            get
            {
                int delay;

                if (m_ParameterDictionary.ContainsKey("DelayedStart") && int.TryParse(m_ParameterDictionary["DelayedStart"], out delay))
                {
                    return delay; 
                }

                return 0; 
            }
        }

        private string GetKeyFromParam(string arg)
        {
            string[] parts = arg.Split(new char[] { ':' });

            if (parts.Length != 2)
                return string.Empty;

            return parts[0]; 
        }

        private string GetValueFromParam(string arg)
        {
            string[] parts = arg.Split(new char[] {':'});

            if (parts.Length != 2)
                return string.Empty; 

            return parts[1]; 
        }
    }
}
